var appPath = getAppPath();	

$(function(){
	initJspCompont();
	initJspGrid();
});

function initJspCompont(){
	$("#selctMemberType").combobox({
		url:appPath+'/com/getCodesForCombox.action',
		valueField:'id',
		textField:'text'
	});
}

function initJspGrid(){
	$("#memberGrid").datagrid({
		url:appPath+'/com/userList.action?userType=1',
		height:'full',
		striped: true,
		fitColumns:true,
		queryParams: getQueryParam(),
		singleSelect:true,
		frozenColumns:[[
		      {field:'op',title:'操作',width:30,align:'center',
		    	  formatter:function(value,rowData,rowIndex){
		    		  if(!rowData.id)return "";
		    			  return "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/pencil.png' title='编辑' onClick='showEdit("+rowIndex+")'/></a>";
		    	  }
		      }
		 ]],
		columns:[[
		     
		      /*{field:'memberId',title:'会员ID',width:80,align:'left',sortable:true},
		      {field:'memberName',title:'会员名称',width:60,align:'left',sortable:true},
		      {field:'memberTypeName',title:'会员类型',width:70,align:'left',sortable:true},
		      {field:'memberStatusName',title:'会员状态',width:60,align:'left',sortable:true},
		      {field:'identityCard',title:'身份证号码',width:100,align:'left',sortable:true},
		      {field:'email',title:'电子邮箱',width:120,align:'center',sortable:true},
		      {field:'mobilephone',title:'手机',width:70,align:'left',sortable:true},
		      {field:'address',title:'所属地区',width:160,align:'left',sortable:true},
		      {field:'createTime',title:'创建时间',width:110,align:'left',sortable:true,formatter:fmtCreateTime},
		      {field:'hotFlag',title:'是否热门企业',width:110,align:'left',sortable:true,formatter:fmtHotFlag}*/
		      {field:'id',title:'会员ID',width:80,align:'left',sortable:true},
		      {field:'username',title:'会员名',width:60,align:'left',sortable:true},
		      /*{field:'realname',title:'真实姓名',width:70,align:'left',sortable:true},
		      {field:'sexname',title:'性别',width:20,align:'left',sortable:true},
		      {field:'email',title:'电子邮箱',width:120,align:'center',sortable:true},
//		      {field:'collecion',title:'收藏景区',width:60,align:'left',sortable:true},
		      {field:'usertypename',title:'会员类型',width:30,align:'left',sortable:true},*/
		      {field:'loginstate',title:'登录状态',width:30,align:'left',sortable:true},
		      {field:'statename',title:'会员状态',width:30,align:'left',sortable:true}
		      ]],
		 hideColumn:['usertype','sex','state'],
		 pagination:true,
		 rownumbers:true,
		 toolbar:[{
				text:'增加认证会员',
				iconCls:'icon-add',
				handler:function(){
					showAddInfo();
				}
			}/*,{
				text:'重置密码',
				iconCls:'icon-reset',
				handler:function(){
					resetPassword();
				}
			},{
				text:'设为热门企业',
				iconCls:'icon-save',
				handler:function(){
					setHotCompany();
				}
			},{
				text:'取消热门企业',
				iconCls:'icon-undo',
				handler:function(){
					cancelHotCompany();
				}
			}*/]
	});
}

function showAdd(){
	$('#selectTypeDlg').dialog({
		buttons:[
			{text:'确定',iconCls:"icon-ok",handler:function(){
				var memberType=$("#selctMemberType").combobox("getValue");
				if(!memberType||memberType.length<=0){
					$.messager.alert('提示信息','请选择会员类型！','warning');
					return;
				}
				$('#selectTypeDlg').dialog('close');
				showAddInfo();
			}},
			{text:'取消',iconCls:"icon-cancel",handler:function(){
				$('#selectTypeDlg').dialog('close');
			}}
		]
	});
	$('#selectTypeDlg').dialog('open');
	//showAddInfo();
}

//设为热门企业
function setHotCompany(){
	var selectMember=$("#memberGrid").datagrid("getSelected");
	if(!selectMember){
		$.messager.alert('提示信息','请选择一个企业！','warning');
		return;
	}
	if(selectMember.memberStatus!="2"||selectMember.memberType!="2"){
		$.messager.alert('提示信息','只有认证的货主会员才能设为热门企业！','warning');
		return;
	}
	var url = appPath+"/member/memberInfo_setHotCompany.do?hotFlag=1&memberId="+selectMember.memberId;
	$.post(url,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','热门企业设置成功。','info');
			doQuery();
		}
	});
}

//取消设置企业
function cancelHotCompany(){
	var selectMember=$("#memberGrid").datagrid("getSelected");
	if(!selectMember){
		$.messager.alert('提示信息','请选择一个企业！','warning');
		return;
	}
	if(selectMember.hotFlag!="1"){
		$.messager.alert('提示信息','请选择一个热门企业！','warning');
		return;
	}
	var url = appPath+"/member/memberInfo_setHotCompany.do?hotFlag=0&memberId="+selectMember.memberId;
	$.post(url,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','热门企业取消成功。','info');
			doQuery();
		}
	});
}

//重置用户密码
function resetPassword(){
	var selectMember=$("#memberGrid").datagrid("getSelected");
	if(!selectMember){
		$.messager.alert('提示信息','请选择要重置密码的会员！','warning');
		return;
	}
	var url = appPath+"/member/memberInfo_reset.do?memberId="+selectMember.memberId;
	$.post(url,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','密码重置成功，重置后的密码为123456','info');
		}
	});
}

/**
 * 展现会员追回页面
 */
function showAddInfo(){
	//var memberType=$("#selctMemberType").combobox("getValue");
	//var memberTypeName = $("#selctMemberType").combobox("getText");
//	if(memberType=="1"){//个人
		$("#personalTable").show();
		$("#companyTable").hide();
		$('#personalForm').form('clear');
		//$("#memberType","#personalForm").val(memberType);
		//$("#memberTypeName","#personalForm").val(memberTypeName);
		$("#memberId","#personalForm").removeClass("readOnlyText");
		$("#memberId","#personalForm").addClass("inputText");
		$("#memberId","#personalForm").removeAttr("readonly");
		
		$("#username","#personalForm").removeClass("readOnlyText");
		$("#username","#personalForm").removeAttr("readonly");
		$("#username","#personalForm").addClass("inputText");
		
		$("#useridTitle","#personalForm").text("");
		$("#userid","#personalForm").hide("");
		$("#notice").hide();
//	}else{//企业
//		$("#personalTable").hide();
//		$("#companyTable").show();
//		$('#companyForm').form('clear');
//		$("#memberType","#companyForm").val(memberType);
//		$("#memberTypeName","#companyForm").val(memberTypeName);
//		$("#memberId","#companyForm").removeClass("readOnlyText");
//		$("#memberId","#companyForm").addClass("inputText");
//		$("#memberId","#companyForm").removeAttr("readonly");
//	}
	
	$("#submitType").val("save");
	$("#addBtn").show();
	$('#addDlg').window({
		title:'增加认证会员',
		iconCls:'icon-add',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#addDlg').window('move',{top:20,left:20});
	$('#addDlg').window('open');
}

/**
 * 展现编辑界面
 * @param {} rowData
 */
function showEdit(rowIndex){
	//清理内容
	var rowData = $("#memberGrid").datagrid("getRows")[rowIndex];
	var memberType =rowData.usertype;
	var memberStatus = rowData.memberStatus;
	if(memberStatus=="1")$("#addBtn").hide();
//	if(memberType=="1"){
		$("#personalTable").show();
		$("#companyTable").hide();
		$('#personalForm').form('clear');
		$('#personalForm').form('load',rowData);
		$("#userid","#personalForm").removeClass("inputText");
		$("#userid","#personalForm").addClass("readOnlyText");
		$("#userid","#personalForm").attr("readonly","readonly");
		$("#useridTitle","#personalForm").text("会员ID");
		$("#userid","#personalForm").show();
		$("#memberTypeName","#personalForm").val(rowData.usertypename);	
		$("#memberType","#personalForm").val(memberType);	
		$("#userid","#personalForm").val(rowData.id);
		$("#username","#personalForm").val(rowData.username);
		$("#username","#personalForm").removeClass("inputText");
		$("#username","#personalForm").addClass("readOnlyText");
		$("#username","#personalForm").attr("readonly","readonly");
		$("#email","#personalForm").val(rowData.email);
		$("#realname","#personalForm").val(rowData.realname);
		$("#sex","#personalForm").val(rowData.sex);
		$("#state","#personalForm").val(rowData.state);
		$("#notice").hide();
//	}else{
//		$("#personalTable").hide();
//		$("#companyTable").show();
//		$('#companyForm').form('clear');
//		$('#companyForm').form('load',rowData);
//		var setupDate = rowData.setupDate;
//		if(setupDate&&setupDate.length>0){
//			$("#setupDate").datebox("setValue",$.DateUtil.format(new Date(setupDate),'yyyy-MM-dd'));
//		}
//		$("#memberId","#companyForm").removeClass("inputText");
//		$("#memberId","#companyForm").addClass("readOnlyText");
//		$("#memberId","#companyForm").attr("readonly","readonly");
//	}
	
	$("#submitType").val("update");
	$("#selctMemberType").combobox("setValue",memberType);
	$('#addDlg').window({
			title:'编辑会员信息',
			iconCls:'icon-edit',
			modal: true,
			shadow: false,
			collapsible:false,
			minimizable:false,
			maximizable:false
		});
	$('#addDlg').window('move',{left:20,top:20});
	$('#addDlg').window('open');
	
}

/**
 * 会员信息保存
 */
function doPostData(){
	var jqFrom=$("#personalForm");
	if(!jqFrom.form('validate')){
		//return;
	}
	var type = $("#submitType").val();
	
	var url ="";
	if(type == "save"){
		url = appPath+'/com/userSave.action';
	}else{
		url = appPath+'/com/userUpdate.action';
	}
	$("#userType").val("1");
    var sendData = jqFrom.serialize();    
	
	$.post(url,sendData,function(data){
		if(data=="success"){
			$.messager.alert('提示信息','会员信息保存成功。','info');
			doQuery();
			cancelPostData();
		}else if(data=="existId"){
			$.messager.alert('提示信息','会员ID已存在，请重新输入!','warning');
		}else if(data=="existEmail"){
			$.messager.alert('提示信息','电子邮箱已存在，请重新输入!','warning');
		}else if(data=="fail"){
			$.messager.alert('提示信息','会员不存在!','warning');
		}else{
			$.messager.alert('提示信息','会员信息保存失败!请联系系统管理员!','error');
		}
	});
}

/**
 * 结束编辑（关闭对话框）
 */
function cancelPostData(){
	$('#addDlg').window('close');
}

/**
 * 获取请求信息
 */
function getQueryParam(){
	var queryObject = {};
	var memberId = $("#queryMemberId").val();
	var memberName=$("#queryMemberName").val();
	if(memberId&&memberId.length>0)queryObject.id = memberId;
	if(memberName&&memberName.length>0)queryObject.username = memberName;
	return queryObject;
}

/**
 * 查询数据
 */
function doQuery(){
	$("#memberGrid").datagrid('unselectAll');
	$("#memberGrid").datagrid('load',getQueryParam());
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.createTime=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}

function fmtHotFlag(value, rowData, rowIndex){
	if(rowData.hotFlag==1)return "是";
	return "否";
}
